package com.manage.controller;

import com.beans.NewProject;
import com.beans.Photo;
import com.beans.SysUser;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.commons.util.StringUtil;
import com.manage.util.QiNiuUtil;
import com.manage.util.UserUtils;
import com.services.NewProjectService;
import com.services.PhotoService;
import com.services.SysUserService;
import com.services.impl.PhotoServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/23
 */
@Controller
@RequestMapping("newProject")
public class NewProjectController extends BaseController {

    @Autowired
    private NewProjectService newProjectService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PhotoService photoService;

    /**
     * 查询列表
     *
     * @param newProject
     * @return
     */
    @RequiresPermissions("newProject:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(NewProject newProject, HttpServletRequest request) {

        newProject.setCreateBy(UserUtils.getUser().getUserId());

        request.setAttribute("newProject", newProject);

        return "newProject/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param newProject
     * @return
     */
    @RequiresPermissions("newProject:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, NewProject newProject) {
        return newProjectService.queryPageByNewProjece(pageList, newProject);
    }


    /**
     * 新增修改
     *
     * @param newProject
     * @return
     */
    @RequiresPermissions("newProject:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(NewProject newProject, Model model) {
        List labelList = new ArrayList();//标签
        List houseTypeList = new ArrayList();//别墅类型

        List<Photo> photoHXList = new ArrayList<Photo>();//户型图
        List<Photo> photoJTList = new ArrayList<Photo>();//交通图
        List<Photo> photoSJList = new ArrayList<Photo>();//实景图
        List<Photo> photoXGList = new ArrayList<Photo>();//效果图

        if (newProject.getProjectId() != null) {
            newProject = newProjectService.queryById(newProject.getProjectId());

            labelList = Arrays.asList(newProject.getLabel().replace("|", ",").split(","));
            houseTypeList = Arrays.asList(newProject.getHouseType().replace("|", ",").split(","));

            Photo photo = new Photo();
            photo.setRelationId(newProject.getProjectId());
            photo.setRelationType(Constants.threeLong);

            //户型图
            photo.setCategory(Constants.one);
            photoHXList = photoService.queryList(photo);
            //交通图
            photo.setCategory(Constants.two);
            photoJTList = photoService.queryList(photo);
            //实景图
            photo.setCategory(Constants.three);
            photoSJList = photoService.queryList(photo);
            //效果图
            photo.setCategory(Constants.four);
            photoXGList = photoService.queryList(photo);

            if (!StringUtil.isEmpty(newProject.getUserIds())) {
                String[] userids = newProject.getUserIds().split(",");
                if (userids.length > 1) {
                    newProject.setUserId1(userids[0]);
                    newProject.setUserId2(userids[1]);
                }
            }

        }

        List users = sysUserService.queryList(new SysUser());

        model.addAttribute("storeUrl", UserUtils.storeUrl);

        model.addAttribute("users", users);

        model.addAttribute("newProject", newProject);

        model.addAttribute("labelList", labelList);
        model.addAttribute("houseTypeList", houseTypeList);

        model.addAttribute("photoHXList", photoHXList);
        model.addAttribute("photoJTList", photoJTList);
        model.addAttribute("photoSJList", photoSJList);
        model.addAttribute("photoXGList", photoXGList);

        return "newProject/edit";
    }

    @RequiresPermissions("newProject:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(NewProject newProject, HttpServletRequest request,
                       @RequestParam(value = "imgFileIds", required = false) String[] imgFileIds,
                       @RequestParam(value = "imgFileDes", required = false) String[] imgFileDes,
                       @RequestParam(value = "imgFileIds1", required = false) String[] imgFileIds1,
                       @RequestParam(value = "imgFileDes1", required = false) String[] imgFileDes1,
                       @RequestParam(value = "imgFileIds2", required = false) String[] imgFileIds2,
                       @RequestParam(value = "imgFileDes2", required = false) String[] imgFileDes2,
                       @RequestParam(value = "imgFileIds3", required = false) String[] imgFileIds3,
                       @RequestParam(value = "imgFileDes3", required = false) String[] imgFileDes3) {
        int result = -1;
        newProject.setCreateBy(UserUtils.getUser().getUserId());

        newProject.setLabel(newProject.getLabel().replace(",", "|"));
        newProject.setHouseType(newProject.getHouseType().replace(",", "|"));

        newProject.setBuildingType(Constants.three);

        if (newProject.getProjectId() == null) {
            result = newProjectService.add(newProject);
        } else {
            result = newProjectService.update(newProject);
        }

        if (result > 0) {
            Photo photo = new Photo();
            //
            photo.setCreateBy(UserUtils.getUser().getUserId());
            photo.setUpdateBy(UserUtils.getUser().getUserId());
            photo.setRelationType(Constants.threeLong);//房源类型

            //删除和更新图片
            PhotoServiceImpl psi = new PhotoServiceImpl();
            psi.photoOfnewProject(photoService, newProject.getProjectId(), photo, imgFileIds, imgFileDes,
                    imgFileIds1, imgFileDes1, imgFileIds2, imgFileDes2, imgFileIds3, imgFileDes3);
            QiNiuUtil.handleQUrl(imgFileIds, imgFileIds1, imgFileIds2, imgFileIds3);
        }

        return "redirect:list";
    }

    @RequiresPermissions("newProject:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long projectId) {
        NewProject newProject = new NewProject();
        newProject.setProjectId(projectId);
        newProject.setUpdateBy(UserUtils.getUser().getUserId());

        int result = newProjectService.delete(newProject);

        //TODO  成功失败结果页面
        return result;
    }

    /**
     * 详情
     *
     * @param newProject
     * @return
     */
    @RequiresPermissions("newProject:detail")
    @RequestMapping("detail")
    public String detail(NewProject newProject, Model model) {
        List labelList = new ArrayList();//标签
        List houseTypeList = new ArrayList();//别墅类型

        List<Photo> photoHXList = new ArrayList<Photo>();//户型图
        List<Photo> photoJTList = new ArrayList<Photo>();//交通图
        List<Photo> photoSJList = new ArrayList<Photo>();//实景图
        List<Photo> photoXGList = new ArrayList<Photo>();//效果图

        if (newProject.getProjectId() != null) {
            newProject = newProjectService.queryById(newProject.getProjectId());

            labelList = Arrays.asList(newProject.getLabel().replace("|", ",").split(","));
            houseTypeList = Arrays.asList(newProject.getHouseType().replace("|", ",").split(","));

            Photo photo = new Photo();
            photo.setRelationId(newProject.getProjectId());
            photo.setRelationType(Constants.threeLong);

            //户型图
            photo.setCategory(Constants.one);
            photoHXList = photoService.queryList(photo);
            //交通图
            photo.setCategory(Constants.two);
            photoJTList = photoService.queryList(photo);
            //实景图
            photo.setCategory(Constants.three);
            photoSJList = photoService.queryList(photo);
            //效果图
            photo.setCategory(Constants.four);
            photoXGList = photoService.queryList(photo);

            if (!StringUtil.isEmpty(newProject.getUserIds())) {
                String[] userids = newProject.getUserIds().split(",");
                if (userids.length > 1) {
                    newProject.setUserId1(userids[0]);
                    newProject.setUserId2(userids[1]);
                }
            }

        }

        List users = sysUserService.queryList(new SysUser());

        model.addAttribute("storeUrl", UserUtils.storeUrl);

        model.addAttribute("users", users);

        model.addAttribute("newProject", newProject);

        model.addAttribute("labelList", labelList);
        model.addAttribute("houseTypeList", houseTypeList);

        model.addAttribute("photoHXList", photoHXList);
        model.addAttribute("photoJTList", photoJTList);
        model.addAttribute("photoSJList", photoSJList);
        model.addAttribute("photoXGList", photoXGList);

        return "newProject/detail";
    }

    /**
     * 不存在  返回true 存在返回false,条件为空返回false
     *
     * @param newProject
     * @param request
     * @return
     */
    @RequestMapping("notExists")
    @ResponseBody
    public Object exists(NewProject newProject, HttpServletRequest request) {

        List<NewProject> neighborhoods = newProjectService.queryExists(newProject);

        if (StringUtil.isEmpty(newProject.getProjectId())) {
            return !(neighborhoods.size() > 0);
        } else if (neighborhoods.size() > 0) {
            NewProject nhb = newProjectService.queryById(newProject.getProjectId());
            if (nhb.getProjectName().equals(newProject.getProjectName())) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}
